package com.example.sign;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lianhua.song
 * @version 1.0
 * @date 2022/3/3 15:58
 */

@Controller
@RequestMapping("test")
public class testController {

    //    生成pdf  返回给前端
    // file  图片base64
    @PostMapping("pdfToFrontEnd")
    public void pdfToFrontEnd(@RequestParam("file") String file, HttpServletResponse response) throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();

        String dataPrix = ""; //base64格式前头
        String data = "";//实体部分数据
        String [] d = file.split("base64,");//将字符串分成数组
        if(d != null && d.length == 2){
            dataPrix = d[0];
            data = d[1];
        }else {
        //文件不正确
        }
        byte[] b = decoder.decodeBuffer(data);

        // // 将html  转换为 PDF
        long time = new Date().getTime();
        // 如果能拿到 html 的 string 或者byte 可以不用读文件
        try (InputStream fis =  Thread.currentThread().getContextClassLoader().getResourceAsStream("test.txt");
             InputStreamReader isr = new InputStreamReader(fis);
             BufferedReader br = new BufferedReader(isr);
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();) {
            StringBuilder sb = new StringBuilder();
            String s = "";
            while ((s = br.readLine()) != null) {
                //每次读取一行
                sb.append(s);
            }

            PDFUtils.createPDF(outputStream, sb.toString());
            float[] keyWords = PDFUtils.getKeyWords(outputStream.toByteArray());
            if(keyWords ==null){
                response.setStatus(500);
                return;
            }
            ByteArrayOutputStream outputStreamPdfByte = new ByteArrayOutputStream();
            PDFUtils.addImgWaterMark(new ByteArrayInputStream(outputStream.toByteArray()),
                    outputStreamPdfByte,
                    new SimpleDateFormat("yyyy 年 MM 月 dd 日").format(new Date()),
                    new ByteArrayInputStream(b),
                    (int) (keyWords[0]), (int) (keyWords[1]), (int) keyWords[2]);

            // 设置response头信息
            response.reset();
            // 改成输出excel文件
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("签名.pdf", "GB2312"));

            ServletOutputStream outputStream1 = response.getOutputStream();
            outputStream1.write(outputStreamPdfByte.toByteArray());
        }
        System.out.println("生成时间花费 ：" + (new Date().getTime() - time));
    }


}
